package com.ruoyi.flowable.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.flowable.controller.base.BaseFlowableController;
import com.ruoyi.flowable.entity.FlowableProcessDefinitionEntity;
import com.ruoyi.flowable.entity.ProcessDefinitionRequest;
import com.ruoyi.flowable.services.ProcessDefinitonService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program:
 * @description: 流程定义Controller
 * @author: jhw
 * @create: 2021-04-08 16:51
 **/
@RestController
@RequestMapping("/process/definition")
public class ProcessDefinitionController extends BaseFlowableController {
    @Autowired
    ProcessDefinitonService definitonService;
    @GetMapping("/list")
    public TableDataInfo listProcessDefinition(ProcessDefinitionRequest processDefinition){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
        if(StringUtils.isNotEmpty(processDefinition.getName())){
            definitionQuery.processDefinitionNameLike(processDefinition.getName());
        }
        definitionQuery.orderByProcessDefinitionKey().asc();
        Long count = definitionQuery.count();
        List<ProcessDefinition> definitions = definitionQuery.listPage((pageDomain.getPageNum()-1)* pageDomain.getPageSize(),pageDomain.getPageSize());
        List<FlowableProcessDefinitionEntity> definitionEntities  = new ArrayList<>();
        for(ProcessDefinition definition :definitions){
            FlowableProcessDefinitionEntity fpd = new FlowableProcessDefinitionEntity(definition,null);
            Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(definition.getDeploymentId()).singleResult();
            fpd.setDeployTime(deployment.getDeploymentTime());
            definitionEntities.add(fpd);
        }
        TableDataInfo dataInfo = getDataTable(definitionEntities);
        dataInfo.setTotal(count);
        return dataInfo;
    }

    @GetMapping("/listMyProcess")
    public TableDataInfo listMyProcess(ProcessDefinitionRequest processDefinition){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
        if(StringUtils.isNotEmpty(processDefinition.getName())){
            definitionQuery.processDefinitionNameLike(processDefinition.getName());
        }
        Long count = definitionQuery.count();
        List<ProcessDefinition> definitions = definitionQuery.orderByProcessDefinitionKey().latestVersion().asc().listPage((pageDomain.getPageNum()-1)* pageDomain.getPageSize(),pageDomain.getPageSize());
        List<FlowableProcessDefinitionEntity> definitionEntities  = new ArrayList<>();
        for(ProcessDefinition definition :definitions){
            FlowableProcessDefinitionEntity fpd = new FlowableProcessDefinitionEntity(definition,null);
            definitionEntities.add(fpd);
        }
        TableDataInfo dataInfo = getDataTable(definitionEntities);
        dataInfo.setTotal(count);
        return dataInfo;
    }
    @Log(title = "流程定义管理", businessType = BusinessType.OTHER)
    @PostMapping("/suspend")
    public AjaxResult suspendDefinition(ProcessDefinitionRequest definition){
        definitonService.suspend(definition.getId(),definition.isSuspendProcessInstances(),definition.getSuspendedDate());
        return AjaxResult.success("挂起成功");
    }
    @Log(title = "流程定义管理", businessType = BusinessType.OTHER)
    @PostMapping("/activate")
    public AjaxResult activateDefinition(ProcessDefinitionRequest definition){
        definitonService.activate(definition.getId(),definition.isSuspendProcessInstances(),definition.getSuspendedDate());
        return AjaxResult.success("激活成功");
    }
    @Log(title = "流程定义管理", businessType = BusinessType.DELETE)
    @DeleteMapping(value = {"/","/{definitionId}"})
    public  AjaxResult deleteDefinition(@PathVariable(value = "definitionId",required = true)String definitionId){
        definitonService.delete(definitionId,false);
       return AjaxResult.success("删除成功");
    }
}
